﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace WebeTerm.Web
{
    public abstract class BaseJSonHttpHandler : BaseHttpHandler
    {
        /// <summary>
        /// 执行用户请示生成JSON字符串
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected abstract object GetJSonResult(HttpContext context);

        /// <summary>
        /// 设置Handler返回数据格式.
        /// </summary>
        /// <value>The type of the content.</value>
        protected override string ContentType
        {
            get
            {
                return @"application/json";
            }
        }

        /// <summary>
        /// 执行客户端请求.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>System.String.</returns>
        protected override string ExecuteHandler(HttpContext context)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(GetJSonResult(context));
        }
    }
}